
<!DOCTYPE html>
<html lang="zh-CN" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>react/利用ES7装饰器优雅的实现分享功能 - 初一蛙鸣</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="初一蛙鸣-前端-小程序-vue-react,利用ES7装饰器优雅的实现分享功能
一、场景小程序分享功能，由于有些页面是需要，有些页面是不需要，不能全局配置。所以我只能在需要分享功能的页面一遍又一遍的复制如下代码。
12345678&amp;#x2F;,"> 
    
    <link rel="alternative" href="atom.xml" title="初一蛙鸣" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    
    
    
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="react/利用ES7装饰器优雅的实现分享功能 - 初一蛙鸣"/>
    <meta name="twitter:description" content="初一蛙鸣-前端-小程序-vue-react,利用ES7装饰器优雅的实现分享功能
一、场景小程序分享功能，由于有些页面是需要，有些页面是不需要，不能全局配置。所以我只能在需要分享功能的页面一遍又一遍的复制如下代码。
12345678&amp;#x2F;,"/>
    
    
    
    
    <meta property="og:site_name" content="初一蛙鸣"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="react/利用ES7装饰器优雅的实现分享功能 - 初一蛙鸣"/>
    <meta property="og:description" content="初一蛙鸣-前端-小程序-vue-react,利用ES7装饰器优雅的实现分享功能
一、场景小程序分享功能，由于有些页面是需要，有些页面是不需要，不能全局配置。所以我只能在需要分享功能的页面一遍又一遍的复制如下代码。
12345678&amp;#x2F;,"/>
    
<link rel="stylesheet" href="/css/diaspora.css">

    <script>window.searchDbPath = "/search.xml";</script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<meta name="generator" content="Hexo 4.2.1"></head>

<body class="loading">
    <span id="config-title" style="display:none">初一蛙鸣</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://lhl946.gitee.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">react/利用ES7装饰器优雅的实现分享功能</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">react/利用ES7装饰器优雅的实现分享功能</h1>
        <div class="stuff">
            <span>三月 26, 2024</span>
            

        </div>
        <div class="content markdown">
            <p>利用ES7装饰器优雅的实现分享功能</p>
<h3 id="一、场景"><a href="#一、场景" class="headerlink" title="一、场景"></a>一、场景</h3><p>小程序分享功能，由于有些页面是需要，有些页面是不需要，不能全局配置。所以我只能在需要分享功能的页面一遍又一遍的复制如下代码。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;分享</span><br><span class="line">onShareAppMessage() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">        title: &quot;来自xxx的分享&quot;,</span><br><span class="line">        path: &#96;&#x2F;pages&#x2F;index&#x2F;index&#96;,</span><br><span class="line">        imageUrl: &#39;https:&#x2F;&#x2F;www.baidu.com&#x2F;logo.png&#39;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="二、思路"><a href="#二、思路" class="headerlink" title="二、思路"></a>二、思路</h3><p>一次偶然的机会，突然想起来，ES7有个叫装饰器的东西，为什么不试一下呢。</p>
<p>1、将onShareAppMessage这个函数抽出来。</p>
<p>2、利用装饰器直接把onShareAppMessage注入到类的原型上。</p>
<h3 id="三、具体步骤"><a href="#三、具体步骤" class="headerlink" title="三、具体步骤"></a>三、具体步骤</h3><p>decorator.ts</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">import Taro, &#123; ComponentClass &#125; from &#39;@tarojs&#x2F;taro&#39;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 分享功能装饰器</span><br><span class="line">export function share(target: ComponentClass) &#123;</span><br><span class="line">    Object.defineProperty(target.prototype, &#39;onShareAppMessage&#39;, &#123;</span><br><span class="line">        value() &#123;</span><br><span class="line">            return &#123;</span><br><span class="line">                title: &quot;来自xx的分享&quot;,</span><br><span class="line">                path: &#96;&#x2F;pages&#x2F;index&#x2F;index&#96;,</span><br><span class="line">                imageUrl: &#39;https:&#x2F;&#x2F;www.baidu.com&#x2F;logo.png&#39;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>页面上使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">import Taro from &quot;@tarojs&#x2F;taro&quot;</span><br><span class="line">import &#123; View &#125; from &quot;@tarojs&#x2F;components&quot;</span><br><span class="line">import &quot;.&#x2F;test.less&quot;</span><br><span class="line">import &#123; share &#125; from &#39;~&#x2F;utils&#x2F;decorator&#39;</span><br><span class="line"></span><br><span class="line">interface Props &#123; &#125;</span><br><span class="line"></span><br><span class="line">interface State &#123; &#125;</span><br><span class="line"></span><br><span class="line">@share</span><br><span class="line">export default class Example extends Taro.Component&lt;Props, State&gt; &#123;</span><br><span class="line">  config: Taro.Config &#x3D; &#123;</span><br><span class="line">    navigationBarTitleText: &quot;测试页面&quot;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  render() &#123;</span><br><span class="line">    return (</span><br><span class="line">      &lt;View&gt;</span><br><span class="line">      &lt;&#x2F;View&gt;</span><br><span class="line">    )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="四、说明"><a href="#四、说明" class="headerlink" title="四、说明"></a>四、说明</h3><p>新建页面的时候，如果需要分享功能，直接在类上面写一个“@share”就行了（当然是你已经import的情况下）。是不是比以前优雅了很多呢。</p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
        
    <div id="gitalk-container" class="comment link"
		data-enable="false"
        data-ae="false"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/typed.js"></script>
<script src="/js/diaspora.js"></script>


<link rel="stylesheet" href="/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/photoswipe/default-skin/default-skin.css">


<script src="/photoswipe/photoswipe.min.js"></script>
<script src="/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
